package codeRandomThoughts.Test77组合;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> combine(int n, int k) {
        int startIndex = 1;
        backTrack(n, k, startIndex);
        return result;
    }

    public void backTrack(int n, int k, int startIndex) {
        //递归终止条件,遍历出了需要的组合,此时path长度等于k
        if (path.size() == k) {
            result.add(new ArrayList<>(path));
            return;
        }

        //单层搜索
        //i可以剪枝
        for (int i = startIndex; i <= (n - (k - path.size()) + 1); i++) {
            path.add(i);
            backTrack(n, k, i + 1);
            path.remove(path.size() - 1);
        }
    }
}
